﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using Autodesk.Revit.DB;
using HYRevitCode.XYZUtility;
using QuickModel.InteractiveOperation;
using Teigha.Core;
using Teigha.TD;
using Transfer.Utils;

namespace QuickModel.DoorAndWindow
{
	public class DoorAndWindowBlockRefInfo : BlockInfo
	{
		public XYZ HandDir
		{
			get
			{
				return this.m_vcHandDir;
			}
		}

		public XYZ FaceDir
		{
			get
			{
				return this.m_vcFaceDir;
			}
		}

		public List<XYZ> RefPoints { get; set; }

		public double Width
		{
			get
			{
				return this.m_dWidth;
			}
		}

		public bool HasAttribute { get; private set; }

		public DoorAndWindowBlockRefInfo(OdDbBlockReference odDbBlockRef, ImportInstance ImportIns) : base(odDbBlockRef, ImportIns)
		{
			this.GetReferPoints();
			this.GetHandDir();
			this.GetFaceDir();
			this.CheckHasAttribute();
			this.GetWidth();
		}

		private void GetWidth()
		{
			bool flag = this.IsTianZhengDoorWindowBlock(this.BlockName);
			if (flag)
			{
				bool flag2 = this.UseScale != null;
				if (flag2)
				{
					this.m_dWidth = Math.Abs(this.UseScale.X);
				}
			}
			else
			{
				try
				{
					OdDbBlockReference odDbBlockReference = this.m_odDbBlockRef.clone() as OdDbBlockReference;
					bool flag3 = odDbBlockReference == null;
					if (!flag3)
					{
						odDbBlockReference.setRotation(0.0);
						OdGeExtents3d odGeExtents3d = new OdGeExtents3d();
						bool flag4 = odDbBlockReference.getGeomExtents(odGeExtents3d) > OdResult.eOk;
						if (!flag4)
						{
							OdGePoint3d odGePoint3d = odGeExtents3d.minPoint();
							OdGePoint3d odGePoint3d2 = odGeExtents3d.maxPoint();
							OdGePoint3d cadPnt = new OdGePoint3d(odGePoint3d2.x, odGePoint3d.y, 0.0);
							Transform totalTransform = base.HostImportIns.GetTotalTransform();
							XYZ xyz = GeometryCommon.ConvetPointFromCadToRevit(odGePoint3d, totalTransform);
							XYZ xyz2 = GeometryCommon.ConvetPointFromCadToRevit(cadPnt, totalTransform);
							this.m_dWidth = Common.FeetToMM(xyz.DistanceTo(xyz2));
						}
					}
				}
				catch (Exception ex)
				{
					Trace.Assert(true, ex.Message);
				}
			}
		}

		private void GetHandDir()
		{
			try
			{
				OdGeVector3d odGeVector3d = new OdGeVector3d(100.0, 0.0, 0.0);
				OdGeVector3d odGeVector3d2 = odGeVector3d.transformBy(this.m_odDbBlockRef.blockTransform());
				XYZ xyz = new XYZ(odGeVector3d2.x, odGeVector3d2.y, 0.0);
				Transform totalTransform = base.HostImportIns.GetTotalTransform();
				XYZ vt = totalTransform.OfVector(xyz).Normalize();
				XYZ vcDivide = XYZUtilityMethod.RotateTo(vt, Math.PI*.5, XYZ.BasisZ);
				this.m_vcHandDir = GeometryCommon.GetDensePointsSideDir(this.RefPoints, this.UseLocationPoint, vcDivide, 1.0);
			}
			catch (Exception)
			{
			}
		}

		private void GetFaceDir()
		{
			try
			{
				this.m_vcFaceDir = GeometryCommon.GetDensePointsSideDir(this.RefPoints, this.UseLocationPoint, this.m_vcHandDir, 1.0);
			}
			catch (Exception)
			{
			}
		}

		private void GetReferPoints()
		{
			try
			{
				List<OdDbCurve> list = new List<OdDbCurve>();
				OdDbBlockTableRecord odDbBlockTableRecord = (OdDbBlockTableRecord)this.m_odDbBlockRef.blockTableRecord().openObject(OpenMode.kForRead);
				OdDbObjectIterator odDbObjectIterator = odDbBlockTableRecord.newIterator();
				odDbObjectIterator.start();
				while (!odDbObjectIterator.done())
				{
					OdDbEntity odDbEntity = odDbObjectIterator.entity(OpenMode.kForRead, false);
					bool flag = odDbEntity == null;
					if (!flag)
					{
						bool flag2 = odDbEntity is OdDbPolyline;
						if (flag2)
						{
							OdDbPolyline odDbPolyline = OdDbPolyline.cast(odDbEntity);
							List<OdDbLine> list2 = new List<OdDbLine>();
							OdRxObjectPtrArray odRxObjectPtrArray = new OdRxObjectPtrArray();
							bool flag3 = odDbPolyline.subExplode(odRxObjectPtrArray) == OdResult.eOk;
							if (flag3)
							{
								foreach (OdRxObject pObj in odRxObjectPtrArray)
								{
									OdDbLine odDbLine = OdDbLine.cast(pObj);
									bool flag4 = odDbLine != null;
									if (flag4)
									{
										OdDbLine item = (OdDbLine)odDbLine.clone();
										list2.Add(item);
									}
								}
							}
							list.AddRange(list2);
						}
						else
						{
							bool flag5 = odDbEntity is OdDbCurve;
							if (flag5)
							{
								list.Add(odDbEntity as OdDbCurve);
							}
						}
					}
					odDbObjectIterator.step();
				}
				List<OdGePoint3d> list3 = new List<OdGePoint3d>();
				foreach (OdDbCurve odDbCurve in list)
				{
					OdGePoint3d odGePoint3d = new OdGePoint3d();
					bool flag6 = odDbCurve.getStartPoint(odGePoint3d) == OdResult.eOk;
					if (flag6)
					{
						list3.Add(odGePoint3d.transformBy(this.m_odDbBlockRef.blockTransform()));
					}
					OdGePoint3d odGePoint3d2 = new OdGePoint3d();
					bool flag7 = odDbCurve.getEndPoint(odGePoint3d2) == OdResult.eOk;
					if (flag7)
					{
						list3.Add(odGePoint3d2.transformBy(this.m_odDbBlockRef.blockTransform()));
					}
				}
				List<XYZ> list4 = new List<XYZ>();
				Transform totalTransform = base.HostImportIns.GetTotalTransform();
				foreach (OdGePoint3d cadPnt in list3)
				{
					list4.Add(GeometryCommon.ConvetPointFromCadToRevit(cadPnt, totalTransform));
				}
				this.RefPoints = list4;
			}
			catch (Exception ex)
			{
				Trace.Assert(true, ex.Message);
			}
		}

		private void CheckHasAttribute()
		{
			try
			{
				OdDbObjectIterator odDbObjectIterator = this.m_odDbBlockRef.attributeIterator();
				odDbObjectIterator.start();
				while (!odDbObjectIterator.done())
				{
					OdDbEntity odDbEntity = odDbObjectIterator.entity(OpenMode.kForWrite, false);
					bool flag = odDbEntity == null;
					if (!flag)
					{
						bool flag2 = odDbEntity is OdDbAttribute;
						if (flag2)
						{
							this.HasAttribute = true;
							return;
						}
					}
					odDbObjectIterator.step();
				}
			}
			catch (Exception ex)
			{
				Trace.Assert(true, ex.Message);
			}
			this.HasAttribute = false;
		}

		private bool IsTianZhengDoorWindowBlock(string strBlockName)
		{
			return strBlockName.ToUpper().Contains("$WINLIB") || strBlockName.ToUpper().Contains("$DORLIB");
		}

		private XYZ m_vcFaceDir = null;

		private XYZ m_vcHandDir = null;

		private double m_dWidth = 0.0;
	}
}
